package Client;

public class CommandHandler {
	private Client client;
	private String[] tokens;
	
	public CommandHandler( Client client ){
		this.client = client;
	}
	
	public void handle(String inline) {
		tokens = inline.split(" ");
		switch( Integer.parseInt(tokens[0])){
			case 210:
				System.out.println("");
				break;
			case 220:
				System.out.println("You succesfully logged in!");
				showHelp();
				client.setLoggedIn(true);
				client.setWaiting( false );
				break;
			case 230:
				System.out.println("You withdrew $" + tokens[1] + ".");
				break;
			case 231:
				System.out.println("You do not have enough money to do that!");
				break;
			case 240:
				System.out.println("You have $" + tokens[1] + " on your bank account.");
				break;
			case 250:
				System.out.println("Have a nice day!");
				client.setLoggedIn(false);
				break;
			case 505:
				System.out.println("an error occured");
				break;
			case 666:
				System.exit(0);
				break;
			default:
				System.out.println("undifined command");
		}
	}
	public void handleInput(String inline){
		if( inline.equals("?") || inline.equals("help")){
			showHelp();
		}
		if( inline.startsWith("login")){
			client.send("CARD " + inline.substring(6));
		}
		if( inline.startsWith("Lookup")){
			client.send("RQST");
		}
		if( inline.startsWith("Logout")){
			client.send("LGTU");
		}
		if( inline.startsWith("Withdraw")){
			client.send("WTDW " + inline.substring( 9 ));
		}
	}
	private void showHelp(){
		System.out.println("\nType the next tokens and requirements to start sn action!");
		System.out.println("'Withdraw <fill in an amount>' to withdraw that amount.");
		System.out.println("'Lookup' to watch your money status.");
		System.out.println("'Logout' to log out");
		System.out.println("Type '?' or 'help' to see these options again");
	}
}
